<?php

/**
 * vip
 * Date: 2021年3月4日
 * Time: 18:06:03
 */

namespace App\Model\BbcModel;

use App\Model\BaseModel\BaseBbcModel;

class Vip extends BaseBbcModel {
    public $table = 'ly_vip';

    public static $vipFrom = [
        0 => '购买',
        1 => '赠送',
        2 => '注册赠送'
    ];

    /**
     * 检查用户在当前店铺是否是黑卡
     */
    public static function check(array $param = []) {
        $has = self::where('expire_time', '>', time())->where('mn_sid', $param['mn_sid'])->where('mid', $param['mid'])->first();
        if ($has) {
            $has = $has->toArray();
            return ['is_vip' => 1, 'expire_time' => $has['expire_time'], 'type' => self::$vipFrom[$has['type']]];
        } else {
            $info = Mnuser::instance()->info(['id' => $param['mn_sid']]);

            if (!empty($info) && $info['status'] == 1 && $info['member_share'] == 1) {
                $list   = Mnuser::instance()->lists(['member_share' => 1, 'status' => 1, 'new_store' => 1, 'pid' => $info['pid'], '_fields_' => 'id']);
                $mn_sid = array_column($list, 'id');
                $has2   = self::where('expire_time', '>', time())->whereIn('mn_sid', $mn_sid)->where('mid', $param['mid'])->select(['id', 'expire_time', 'type'])->orderBy('expire_time', 'desc')->first();
                if ($has2) {
                    return ['is_vip' => 1, 'expire_time' => $has2['expire_time'], 'type' => self::$vipFrom[$has2->type]];
                } else {
                    $has2_expired = self::where('expire_time', '<', time())->where('expire_time', '>', 0)->whereIn('mn_sid', $mn_sid)->where('mid', $param['mid'])->select(['id', 'expire_time', 'type'])->orderBy('expire_time', 'desc')->first();
                    if ($has2_expired) {
                        return ['is_vip' => 0, 'expire_time' => 0, 'expired' => 1, 'expired_time' => $has2_expired->expire_time];
                    } else {
                        return ['is_vip' => 0, 'expire_time' => 0, 'expired' => 0];
                    }
                }
            } else {
                $has_expired = self::where('expire_time', '<', time())->where('expire_time', '>', 0)->where('mn_sid', $param['mn_sid'])->where('mid', $param['mid'])->first();
                if ($has_expired) {
                    return ['is_vip' => 0, 'expire_time' => 0, 'expired' => 1, 'expired_time' => $has_expired->expire_time];
                } else {
                    return ['is_vip' => 0, 'expire_time' => 0, 'expired' => 0];
                }
            }
        }

    }
}